﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class DropDownListDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public DropDownListDAL()
        {

        }
        public IList<DropDownListInfo> LoadListScopes()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<DropDownListInfo> infoList = new List<DropDownListInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT SCOPE_SEQ, SCOPE_NAME"
                    + " FROM HRM_MP_SCOPES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + " ORDER BY SCOPE_NAME";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        DropDownListInfo info = new DropDownListInfo();
                        info.Sequence = Convert.IsDBNull(dr[i]["SCOPE_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["SCOPE_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["SCOPE_NAME"]) ? "" : (dr[i]["SCOPE_NAME"]).ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<DropDownListInfo> LoadListJobsiteLevels()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<DropDownListInfo> infoList = new List<DropDownListInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT JOB_SITE_LEVEL_SEQ, JOB_SITE_LEVEL_NO, JOB_SITE_LEVEL_NAME"
                    + " FROM HRM_MP_JOB_SITE_LEVELS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + " ORDER BY JOB_SITE_LEVEL_NO";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        DropDownListInfo info = new DropDownListInfo();
                        info.Sequence = Convert.IsDBNull(dr[i]["JOB_SITE_LEVEL_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["JOB_SITE_LEVEL_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["JOB_SITE_LEVEL_NAME"]) ? "" : (dr[i]["JOB_SITE_LEVEL_NAME"]).ToString();
                        
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<DropDownListInfo> LoadListJobsiteTypes()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<DropDownListInfo> infoList = new List<DropDownListInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT JOB_SITE_TYPE_SEQ, JOB_SITE_TYPE_NAME, REMARKS"
                    + " FROM HRM_MP_JOB_SITE_TYPES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + " ORDER BY JOB_SITE_TYPE_SEQ";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        DropDownListInfo info = new DropDownListInfo();
                        info.Sequence = Convert.IsDBNull(dr[i]["JOB_SITE_TYPE_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["JOB_SITE_TYPE_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["JOB_SITE_TYPE_NAME"]) ? "" : (dr[i]["JOB_SITE_TYPE_NAME"]).ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<DropDownListInfo> LoadListDistricts(Nullable<int> ProvinceSeq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<DropDownListInfo> infoList = new List<DropDownListInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@ProvinceSeq", ProvinceSeq);

                strSQL = "SELECT d.DISTRICT_SEQ, d.DISTRICT_NAME"
                    + ", a.AMP_NAME, p.PROV_NAME"
                    + ", CONCAT(d.DISTRICT_NAME, ' ', a.AMP_NAME, ' ', p.PROV_NAME) AS DISTRICT_TEXT"
                    + " FROM MAS_DISTRICTS d"
                    + " INNER JOIN MAS_AMPHURS a ON d.AMP_SEQ = a.AMP_SEQ"
                    + " INNER JOIN MAS_PROVINCES p ON a.PROV_SEQ = p.PROV_SEQ"
                    + " WHERE d.RECORD_STATUS not like @DeleteRecord"
                        + (ProvinceSeq.HasValue ? " AND a.PROV_SEQ = @ProvinceSeq" : "")
                    + " ORDER BY DISTRICT_NAME";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        DropDownListInfo info = new DropDownListInfo();
                        info.Sequence = Convert.IsDBNull(dr[i]["DISTRICT_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["DISTRICT_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["DISTRICT_TEXT"]) ? "" : (dr[i]["DISTRICT_TEXT"]).ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<DropDownListInfo> LoadListProvinces(Nullable<int> Regions)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<DropDownListInfo> infoList = new List<DropDownListInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Regions", Regions);

                strSQL = "SELECT PROV_SEQ, PROV_NAME, PROV_NAME_ENG"
                    + " FROM MAS_PROVINCES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                        + (Regions.HasValue ? " AND REGION_SEQ = @Regions" : "")
                    + " ORDER BY PROV_NAME";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        DropDownListInfo info = new DropDownListInfo();
                        info.Sequence = Convert.IsDBNull(dr[i]["PROV_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["PROV_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["PROV_NAME"]) ? "" : (dr[i]["PROV_NAME"]).ToString();
              
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<DropDownListInfo> LoadListCountry()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<DropDownListInfo> infoList = new List<DropDownListInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT COUNTRY_SEQ, COUNTRY_NAME"
                    + " FROM MAS_COUNTRIES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + " ORDER BY COUNTRY_NAME";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        DropDownListInfo info = new DropDownListInfo();
                        info.Sequence = Convert.IsDBNull(dr[i]["COUNTRY_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["COUNTRY_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["COUNTRY_NAME"]) ? "" : (dr[i]["COUNTRY_NAME"]).ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<DropDownListInfo> LoadListCountryZones()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<DropDownListInfo> infoList = new List<DropDownListInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT ZONE_SEQ, ZONE_NAME"
                    + " FROM MAS_COUNTRY_ZONES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + " ORDER BY ZONE_NAME";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        DropDownListInfo info = new DropDownListInfo();
                        info.Sequence = Convert.IsDBNull(dr[i]["ZONE_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["ZONE_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["ZONE_NAME"]) ? "" : (dr[i]["ZONE_NAME"]).ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
    }
}
